package com.xuecheng.learning.service.impl;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.learning.service.MyLearningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MyLearningServiceImpl implements MyLearningService {

    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    MediaServiceClient mediaServiceClient;
    @Autowired
    MyCourseTablesService myCourseTablesService;



    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        //查询课程
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if(coursepublish==null){
            XueChengPlusException.cast("课程信息不存在");
        }
        //是否登录
        if (userId!=null){
            //登陆成功,判断是否有学习资格
            XcCourseTablesDto courseTablesDto = myCourseTablesService.getLearningStatus(userId, courseId);
            String status=courseTablesDto.getLearnStatus();
            if (status.equals("702002")){  // {"code":"702002","desc":"没有选课或选课后没有支付"},
                return RestResponse.validfail("没有选课或选课后没有支付");
            }else if (status.equals("702003")){  // {"code":"702003","desc":"已过期需要申请续期或重新支付"}
                return RestResponse.validfail("已过期需要申请续期或重新支付");
            }else {         //{"code":"702001","desc":"正常学习"},
                //有资格，获取视频
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            }
        }
        //无登录，判断是否为收费课程
        String charge = coursepublish.getCharge();
        if (charge.equals("201001")){
            return RestResponse.validfail("该课程为收费项目，请登录后支付即可观看");
        }
        return mediaServiceClient.getPlayUrlByMediaId(mediaId);
    }


}
